package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.RbacOperatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 * 操作码管理
 */
@Controller
@RequestMapping("rbacOperating")
public class RbacOperatingController {

    @Autowired
    private RbacOperatingService rbacOperatingService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "/rbac/operating";
    }

    @RequestMapping(value = "/view",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> view(HttpServletRequest request,int page,int rows){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        PageHelper.startPage(page, rows);
        params.put("title", request.getParameter("title"));
        List<Map<String, Object>> list = rbacOperatingService.getRbacOperating(params);
        PageInfo pageInfo = new PageInfo(list);
        result.put("rows",list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    /**
     * 添加操作
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String,Object> add(HttpServletRequest request){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("op_id",null);
        params.put("opName",request.getParameter("opName"));
        params.put("opCoding",request.getParameter("opCoding"));
        try {
            rbacOperatingService.addRbacOperating(params);
        } catch (Exception e) {
            result.put("errormsg","添加失败");
        }
        return result;
    }

    /**
     * 修改操作
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST, produces = "text/htm;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(HttpServletRequest request,int id){
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("opId",Integer.parseInt(request.getParameter("id")));
        params.put("opName",request.getParameter("opName"));
        params.put("opCoding",request.getParameter("opCoding"));
        try {
            rbacOperatingService.updateRbacOperating(params);
        } catch (Exception e) {
            result.put("errormsg", "修改失败");
        }
        return result;
    }

    /**
     * 删除管理者
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(int id){
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            rbacOperatingService.deleteRbacOperating(id);
        } catch (Exception e) {
            result.put("errormsg", "删除失败");
        }
        return result;
    }
}
